import tkinter as tk
from tkinter import messagebox

# 模拟数据库
users_db = {}

# 用户类
class User:
    def __init__(self, username, password, balance=0, loan=0):
        self.username = username
        self.password = password
        self.balance = balance
        self.loan = loan
        self.is_active = True

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            return False
        else:
            self.balance -= amount
            return True

    def report_lost(self):
        self.is_active = False

    def repay_loan(self, amount):
        if amount > self.balance:
            return False
        else:
            self.balance -= amount
            self.loan -= amount
            if self.loan < 0:
                self.loan = 0
            return True

# 银行管理系统类
class BankManagementSystem:
    def __init__(self, root):
        self.root = root
        self.root.title("银行管理系统")

        self.current_user = None

        self.main_screen()

    def main_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="欢迎使用银行管理系统", font=("Arial", 16)).pack()
        tk.Button(self.root, text="登录", command=self.login_screen).pack(pady=10)
        tk.Button(self.root, text="注册", command=self.register_screen).pack(pady=10)

    def clear_screen(self):
        for widget in self.root.winfo_children():
            widget.destroy()

    def register_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="注册新账户", font=("Arial", 16)).pack(pady=10)

        tk.Label(self.root, text="用户名:").pack()
        username_entry = tk.Entry(self.root)
        username_entry.pack()

        tk.Label(self.root, text="密码:").pack()
        password_entry = tk.Entry(self.root, show="*")
        password_entry.pack()

        tk.Button(self.root, text="注册", command=lambda: self.register(username_entry.get(), password_entry.get())).pack(pady=10)
        tk.Button(self.root, text="返回", command=self.main_screen).pack(pady=10)

    def register(self, username, password):
        if username in users_db:
            messagebox.showerror("错误", "用户名已存在")
        else:

            users_db[username] = User(username, password)
            messagebox.showinfo("成功", "注册成功")
            self.main_screen()

    def login_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="用户登录", font=("Arial", 16)).pack(pady=10)

        tk.Label(self.root, text="用户名:").pack()
        username_entry = tk.Entry(self.root)
        username_entry.pack()

        tk.Label(self.root, text="密码:").pack()
        password_entry = tk.Entry(self.root, show="*")
        password_entry.pack()

        tk.Button(self.root, text="登录", command=lambda: self.login(username_entry.get(), password_entry.get())).pack(pady=10)
        tk.Button(self.root, text="返回", command=self.main_screen).pack(pady=10)

    def login(self, username, password):
        user = users_db.get(username)
        if user and user.password == password:
            if user.is_active:
                self.current_user = user
                self.account_screen()
            else:
                messagebox.showerror("错误", "账户已挂失，无法登录")
        else:
            messagebox.showerror("错误", "用户名或密码错误")

    def account_screen(self):
        self.clear_screen()

        tk.Label(self.root, text=f"欢迎, {self.current_user.username}", font=("Arial", 16)).pack(pady=10)

        tk.Label(self.root, text=f"账户余额: ¥{self.current_user.balance}").pack(pady=10)
        tk.Label(self.root, text=f"贷款金额: ¥{self.current_user.loan}").pack(pady=10)

        tk.Button(self.root, text="存款", command=self.deposit_screen).pack(pady=5)
        tk.Button(self.root, text="取款", command=self.withdraw_screen).pack(pady=5)
        tk.Button(self.root, text="挂失", command=self.report_lost_screen).pack(pady=5)
        tk.Button(self.root, text="还贷", command=self.repay_loan_screen).pack(pady=5)
        tk.Button(self.root, text="退出", command=self.logout).pack(pady=10)

    def deposit_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="存款", font=("Arial", 16)).pack(pady=10)

        tk.Label(self.root, text="存款金额:").pack()
        amount_entry = tk.Entry(self.root)
        amount_entry.pack()

        tk.Button(self.root, text="确认", command=lambda: self.deposit(amount_entry.get())).pack(pady=10)
        tk.Button(self.root, text="返回", command=self.account_screen).pack(pady=10)

    def deposit(self, amount):
        try:
            amount = float(amount)
            self.current_user.deposit(amount)
            messagebox.showinfo("成功", f"存款成功，新余额为: ¥{self.current_user.balance}")
            self.account_screen()
        except ValueError:
            messagebox.showerror("错误", "请输入有效的金额")

    def withdraw_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="取款", font=("Arial", 16)).pack(pady=10)

        tk.Label(self.root, text="取款金额:").pack()
        amount_entry = tk.Entry(self.root)
        amount_entry.pack()

        tk.Button(self.root, text="确认", command=lambda: self.withdraw(amount_entry.get())).pack(pady=10)
        tk.Button(self.root, text="返回", command=self.account_screen).pack(pady=10)

    def withdraw(self, amount):
        try:
            amount = float(amount)
            if self.current_user.withdraw(amount):
                messagebox.showinfo("成功", f"取款成功，新余额为: ¥{self.current_user.balance}")
            else:
                messagebox.showerror("错误", "余额不足")
            self.account_screen()
        except ValueError:
            messagebox.showerror("错误", "请输入有效的金额")

    def report_lost_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="挂失账户", font=("Arial", 16)).pack(pady=10)
        tk.Label(self.root, text="确定要挂失此账户吗？").pack(pady=10)

        tk.Button(self.root, text="确认", command=self.report_lost).pack(pady=10)
        tk.Button(self.root, text="返回", command=self.account_screen).pack(pady=10)

    def report_lost(self):
        self.current_user.report_lost()
        messagebox.showinfo("成功", "账户已挂失")
        self.logout()

    def repay_loan_screen(self):
        self.clear_screen()

        tk.Label(self.root, text="还贷", font=("Arial", 16)).pack(pady=10)

        tk.Label(self.root, text="还款金额:").pack()
        amount_entry = tk.Entry(self.root)
        amount_entry.pack()

        tk.Button(self.root, text="确认", command=lambda: self.repay_loan(amount_entry.get())).pack(pady=10)
        tk.Button(self.root, text="返回", command=self.account_screen).pack(pady=10)

    def repay_loan(self, amount):
        try:
            amount = float(amount)
            if self.current_user.repay_loan(amount):
                messagebox.showinfo("成功", f"还贷成功，新余额为: ¥{self.current_user.balance}，剩余贷款: ¥{self.current_user.loan}")
            else:
                messagebox.showerror("错误", "余额不足")
            self.account_screen()
        except ValueError:
            messagebox.showerror("错误", "请输入有效的金额")

    def logout(self):
        self.current_user = None
        self.main_screen()

# 主程序
if __name__ == "__main__":
    root = tk.Tk()
    app = BankManagementSystem(root)
    root.mainloop()
